; CodedAlarm.ASM
;---------------|-------|-----------------------|------------------------------------------------
;Predefine Register Operations
;---------------|-------|-----------------------|------------------------------------------------
; 00.01 This Code Starts up the 16F871, boots the 16x4 LCD 8Bit and Beeps Sounder on RE2 two times
; 00.02 This Code Starts up the 16F871, boots the 16x4 LCD 4Bit and Beeps Sounder on RE2 two times
; 00.03 Basic Keyboard Interface 
; 00.04	Read a password into an array #=enter *=del
; 00.05 Enable basic menu 
; 00.06	Set-up and use TMR0
; 00.07 Allow setting the alarm with basic visual exit timer
; 00.08	Include Hex Byte to Dec Conversion for timer output
; 00.09 Enable timer input menus to allow the entry and exit times to be changed
; 00.0A	Rearrange Menu structures
; 00.0B Introduce Bell Out, Zone 1 (Alarm Zone) and Zone 2 (Entry/Exit) Logic
; 00.0C	Add EEPROM read/write and reset to defaults when input pin active or on EEPROM containing FF's.
; 00.0D Add code check requirements for code change and tidy up screens
; 00.0E Tidy up code 
; 00.0F Zone 2 trigger on Zone 1 time down
; 00.10 Pre-sets loaded into flash on first run
; 00.11 Use New Piezo located across state change LED
; 00.12 Use Spare Link to hide entered numbers when made
; 00.13 Added Key de-bounce and fixed * clearing problem
; 00.14 Change Exit Sound to constant while zone 1 or zone 2 is active on exit
;		Extend system to allow two user codes
;---------------|-------|-----------------------|------------------------------------------------
;Predefine PIC and hardware settings
;---------------|-------|-----------------------|------------------------------------------------
	LIST            P=16F871

; The following line embeds configuration data into the PIC micro-controller

	#include	<p16f871.inc>	; processor specific variable definitions
	
	__CONFIG _CP_OFF & _WDT_OFF & _BODEN_OFF & _PWRTE_ON & _XT_OSC & _WRT_ENABLE_ON & _LVP_OFF & _CPD_OFF


;---------------|-------|-----------------------|------------------------------------------------
;Predefine Register Operations
;---------------|-------|-----------------------|------------------------------------------------
CFILE0			EQU 	20H						;Counter files used on delay loops
CFILE1			EQU		21H    					;
WKFILE			EQU		22H						;General purpose scratch files
GPFILE0			EQU		23H						;
GPFILE1			EQU		24H						;
GPFILE2			EQU		25H						;
GPFILE3			EQU		26H						;


TMPDATA			EQU		27H						;Keyboard scanning variables
READKEY			EQU		28H
KEYROW			EQU		29H
KEYCOL			EQU		30H

TMP0			EQU		31H						;Temporary array to hold entered code
TMP1			EQU		32H
TMP2			EQU		33H
TMP3			EQU		34H
TMP4			EQU		35H
TMP5			EQU		36H
TMP6			EQU		37H
TMP7			EQU		38H

PASSA0			EQU		39H						;1st Valid Code array
PASSA1			EQU		3AH
PASSA2			EQU		3BH
PASSA3			EQU		3CH
PASSA4			EQU		3DH
PASSA5			EQU		3EH
PASSA6			EQU		3FH
PASSA7			EQU		40H

PASSB0			EQU		41H						;2nd Valid Code array
PASSB1			EQU		42H
PASSB2			EQU		43H
PASSB3			EQU		44H
PASSB4			EQU		45H
PASSB5			EQU		46H
PASSB6			EQU		47H
PASSB7			EQU		48H

WRITEPOINTER	EQU		49H						;Used to point into the password array
WRITECHAR		EQU		4AH						;Used to hold the character to be saved in the array
SYSFLAGS_A		EQU		4BH						;Flags used by software
SYSFLAGS_B		EQU		4CH						;Flags used by software												

iCOUNTER		EQU		4EH						;Internal counter used to slow interrupt events
EVENTSECCNTR	EQU		4FH						;Counter used to time system events 1 clock a second
EVENTMINCNTR	EQU		51H						;Counter used to time minutes count

W_TEMP			EQU		52H						;Temporary register to hold W during interrupt
STATUS_TEMP		EQU 	53H						;Temporary register to hold STATUS during interrupt

HEXBT			EQU		54H						;Used in HEX Byte to Dec Conversion
EUNIT			EQU		55H							
ETENS			EQU		56H
EHUNS			EQU		57H
AUNIT			EQU		58H							
ATENS			EQU		59H
AHUNS			EQU		5AH
CINDEX			EQU		5BH
CPOINTER		EQU		5CH

ENTTIME			EQU		5DH
EXTTIME			EQU		5EH
ALMTIME			EQU		5FH

FADDR			EQU		60H
FDATA			EQU		61H

;---------------|-------|-----------------------|------------------------------------------------
;Predefine program Equivalents
;---------------|-------|-----------------------|------------------------------------------------

BEEPDELAY		EQU		H'EF'


;---------------|-------|-----------------------|------------------------------------------------
;Hardware Specific Equivalents
;---------------|-------|-----------------------|------------------------------------------------
RS				EQU		0						;LCD control pins
EN				EQU		1
MUXEN 			EQU  	2

UNSET			EQU		5						;Assign Port B pins to indicate the alarm state
STATECHG		EQU		6						;Outputs
ALMSET			EQU		7
ALARM			EQU		4

ZONE1			EQU		3						;Assign Port B pins
ZONE2			EQU		2						;Inputs
FRESET			EQU		1						
HIDECHR			EQU		0
												;SYSFLAGS_A
ALARMUNSET		EQU		0						;Used to indicate that the system is unset												
ALARMSETTING	EQU		1						;Used to indicate that the timer is timing a setting event
ALARMSET		EQU		2						;Used to indicate that the system is set
ALARMENTRY		EQU		3						;Used to indicate that the timer is timing a entry event
ALARMTIMING		EQU		4						;Used to indicate that the timer is timing a alarm event
FLASHBAD		EQU		5						;Used to flag that he contents of the flash are bad e.g. FF
KEYABORT		EQU		6						;Used to force key entry to end with no result
KEYFIN			EQU		7						;Used by key entry to end when # is pressed by user
												
												;SYSFLAGS_B
HIDEINKEYS		EQU		0						;Used to hide Key Entry
	
;---------------|-------|-----------------------|------------------------------------------------
;Micro-controller Core References
;---------------|-------|-----------------------|------------------------------------------------
MSB				EQU		7						;GP MSB & LSB
LSB     		EQU		0
W       		EQU 	0						;WORKING & FILE IDENTS
F       		EQU 	1
RP0     		EQU 	5						;LOCATION OF REG GROUP SWITCH IN OPTION REG.
RP1     		EQU 	6						;LOCATION OF REG GROUP SWITCH IN OPTION REG.
IRP				EQU		7
PCL				EQU		2
STATUS			EQU		3						;SET VARIABLE NAME OF STAT REG.
CARRY   		EQU		0						;IDENTS FOR FLAG REGISTERS
Z  				EQU 	2

EEPGD			EQU		7
ERD				EQU		0
WREN 			EQU		2

EECON1			EQU  	H'18C'
EECON2			EQU  	H'18D'
EEDATA			EQU  	H'10C'
EEADR 			EQU  	H'10D'
INTCON			EQU		H'0B'
INTCON1			EQU		H'18B'

FSR				EQU		04H
INDF			EQU		00H

;***************|*******|***********************|************************************************
;Hardware Start up
;***************|*******|***********************|************************************************
				ORG  	0x0    							;Reset Vector
				GOTO  	INIT    						;GOTO start of program
;***************|*******|***********************|************************************************
;Interrupt Timing Routine
;***************|*******|***********************|************************************************		
				ORG		0x4						;Interrupt Vector
				MOVWF 	W_TEMP 					;*Copy W to a Temporary Register ; regardless of current bank
				SWAPF 	STATUS,W 				;*Swap STATUS nibbles and place; into W register
				MOVWF 	STATUS_TEMP 			;*Save STATUS to a Temporary register; in Bank0 	
		
				BTFSC	iCOUNTER,2				;drive state led when interrupt is running
				GOTO	iLOFF					;flash state change lamp		
				BSF		PORTB,STATECHG			
				GOTO	jiLEDS
iLOFF			BCF		PORTB,STATECHG
		
jiLEDS			DECF	iCOUNTER,F				;20H/32D Interrupt cycles gives about a 1 second
				MOVFW	iCOUNTER				;Decrement Cycle on event counter
				BTFSS	STATUS,Z
				GOTO	JMPSEC
				MOVLW	00FH
				MOVWF	iCOUNTER
				MOVFW	EVENTSECCNTR			;Decrement as long as counter is not zero
				BTFSS	STATUS,Z
				DECF	EVENTSECCNTR,F
		
iT0				BTFSS	SYSFLAGS_A,ALARMSETTING	;if alarm setting and counter at zero set the alarm
				GOTO	iT1										
				MOVFW	EVENTSECCNTR						
				BTFSC	STATUS,Z
				CALL	SYSTEMSET
				GOTO	JMPSEC
		
iT1				BTFSS	SYSFLAGS_A,ALARMENTRY	;if alarm entry and counter at zero set the bell active
				GOTO	iT2
				MOVFW	EVENTSECCNTR						
				BTFSC	STATUS,Z
				CALL	SYSTEMALARM
				GOTO	JMPSEC
		
iT2				BTFSS	SYSFLAGS_A,ALARMTIMING	;if alarm active and counter at zero clear the bell active
				GOTO	JMPSEC
		
				MOVFW	EVENTMINCNTR			;Check minute					
				BTFSC	STATUS,Z						
				GOTO	LSTMIN
				MOVFW	EVENTSECCNTR			;If not last minute then check second	
				BTFSS	STATUS,Z
				GOTO	JMPSEC					;Reload when sec is zero and decrement mins
				MOVLW	03BH						
				MOVWF	EVENTSECCNTR
				DECF	EVENTMINCNTR,F	
				GOTO	JMPSEC	
		
LSTMIN			MOVFW	EVENTSECCNTR			;check seconds and halt on zero	
				BTFSC	STATUS,Z
				CALL	SYSTEMSET
		
JMPSEC			SWAPF 	STATUS_TEMP,W 			;*Swap original STATUS register value; into W (restores original bank)
				MOVWF 	STATUS 					;*Restore STATUS register from; W register
				SWAPF 	W_TEMP,F 				;*Swap W_Temp nibbles and return; value to W_Temp
				SWAPF 	W_TEMP,W 				;*Swap W_Temp to W to restore original; W value without affecting STATUS 

				BCF		INTCON,T0IF				;Clear interrupt flag
				RETFIE
		
;***************|*******|***********************|************************************************
;Lookup Tables
;***************|*******|***********************|************************************************
;Lookup for Keyboard
KEYTABLE		addwf  PCL,F					;placed low down in memory to prevent program counter roll overs
				dt  A'1',A'2',A'3',A'X',A'4',A'5',A'6',A'X',A'7',A'8',A'9',A'X',A'*',A'0',A'#',A'X'
				RETURN
				
;Lookup for Fast Hex to Dec conversion				
CONTABLE		addwf  PCL,F					;placed low down in memory to prevent program counter roll overs
				dt  01H,02H,04H,08H,06H,02H,04H,08H,00H,00H,00H,00H,01H,03H,06H,02H
				RETURN		
					
;***************|*******|***********************|************************************************
;Initialise PIC
;***************|*******|***********************|************************************************
INIT			CALL	PAGE1          			;SELECT REGISTER BANK 1

				MOVLW	B'11111111'				;
				MOVWF	TRISA^80H				;SET INTO PORTA CONTROL
	  	
				MOVLW	B'00001111'  			;
				MOVWF	TRISB^80H				;SET INTO PORTB CONTROL
		
				MOVLW	B'00000000'				;
				MOVWF	TRISC^80H				;SET INTO PORTC CONTROL
	  	
				MOVLW	B'11110000'  			;
				MOVWF	TRISD^80H				;SET INTO PORTD CONTROL
	
				MOVLW	B'00000000'  			;
				MOVWF	TRISE^80H				;SET INTO PORTE CONTROL

				MOVLW 	0x06 
				MOVWF 	ADCON1^80H
	
				
				CLRF	OPTION_REG^80H
				BSF		OPTION_REG^80H,PS0		;SET - Divide by 256
				BSF		OPTION_REG^80H,PS1		;SET
				BSF		OPTION_REG^80H,PS2		;SET
				BCF		OPTION_REG^80H,PSA      ;CLR - Pre-scaler assigned to Timer 0   
				BCF		OPTION_REG^80H,T0CS  	;CLR - Internal instruction Cycle is 
												;      source to pre-scaler module
												;*One instruction cycle consists of 
												;four oscillator periods*(p105 DS30569A)
												;therefore
												;4Mz Xtal = 1Mhz instruction cycle
												;with 1Mhz into Pre scaler this is
												;1000000/256 = 3906.25 cycles/sec
												;this is then fed into the Timer0 module and this interrupts 
												;on 00 - FF roll-over. therefore 
												;3906.25/256 = 15.2587890625 interrupt/sec
													
		
				CLRF	INTCON					;CLEAR ALL INTERUPT TYPES
				BCF		INTCON,T0IE				;DISENABLE TMR0 INTERUPTS
				BSF		INTCON,GIE				;ENABLE INTERUPTS
		
				CALL	PAGE0					;SELECT REGISTER BANK 0

				
				CLRF	PORTA					;SET ALL OUTPUTS OF PORT A LOW
				CLRF	PORTB					;SET ALL OUTPUTS OF PORT B LOW
				CLRF	PORTC					;SET ALL OUTPUTS OF PORT C LOW
				CLRF	PORTD					;SET ALL OUTPUTS OF PORT D LOW
				CLRF	PORTE					;SET ALL OUTPUTS OF PORT E LOW

				MOVLW	B'00000000'  			;
				MOVWF	PORTB
				
;***************|*******|***********************|************************************************
;Start up and Flash Test
;***************|*******|***********************|************************************************
STARTUP			CALL	INTDSP					;Start-up LCD
				CALL	CLRLCD	
				CALL	LCDOFF
				CALL	BOOTLOGO				;Show logo

				MOVLW	BEEPDELAY 				;Start-up Beeps  		
				CALL  	VDELAY
				CALL  	HBEEP
				MOVLW	BEEPDELAY 			     		
				CALL  	VDELAY
				CALL  	HBEEP
				
				CALL  	MDELAY					;Hold boot logo on screen
				CALL  	MDELAY
				CALL  	MDELAY
				CALL 	MDELAY
				CALL	MDELAY
	
				BTFSC	PORTB,FRESET			;Check if flash reset link is made
				GOTO	FLSHBAD
				
				CALL	CHKFLSH					;Check Flash for Blanks FF's
				BTFSS	SYSFLAGS_A,FLASHBAD
				GOTO	FLSHOK

FLSHBAD			BCF		SYSFLAGS_B,HIDEINKEYS
				CALL	HBEEP
				MOVLW	01EH					;Load pre-sets
				MOVWF	EXTTIME		
				MOVLW	01EH					;Load pre-sets
				MOVWF	ENTTIME	
				MOVLW	0AH						;Load pre-sets
				MOVWF	ALMTIME
				CALL	SETCODEA
				CALL	SETCODEB
				CALL	WRFLSH					;Presets are loaded into Flash on first run
				GOTO	FLSRFIN
	
FLSHOK			CALL	RDFLSH
				
FLSRFIN			BCF		SYSFLAGS_B,HIDEINKEYS
				BTFSC	PORTB,HIDECHR			;Set flag if hide keys link is made
				BSF		SYSFLAGS_B,HIDEINKEYS

				BCF		INTCON,T0IF				;Clear interrupt flag

				CLRF	EVENTSECCNTR			;Clear Event Counter
				MOVLW	00FH					;Pre load internal counter
				MOVWF	iCOUNTER				;
				
;***************|*******|***********************|************************************************	
;Main Alarm Routine
;***************|*******|***********************|************************************************						
MAIN			CALL	SYSTEMUNSET				;Set system state 
				CALL	PRNMNMENU				;Print basic start menu
				
				CALL	SCANKEYS				;Check for user entry
				MOVWF	GPFILE0
				XORLW	01H
				BTFSC	STATUS,Z
				CALL	SETSYS					;if user presses 1 then start setting
				
				MOVFW	GPFILE0
				XORLW	02H
				BTFSC	STATUS,Z
				CALL	SYS						;if user presses 2 then go to setting menu
				GOTO	MAIN
				
;***************|*******|***********************|************************************************	
;System Menu Subs
;***************|*******|***********************|************************************************				
;---------------|-------|-----------------------|------------------------------------------------
;System Setting Menu
;---------------|-------|-----------------------|------------------------------------------------	
SYS				BCF		SYSFLAGS_B,HIDEINKEYS	;make key entry visible

				CALL	CLRLCD	
				CALL	LCDOFF
				CALL	PRNSYMENU
				CALL	SCANKEYS
				
				MOVWF	GPFILE0
				XORLW	01H
				BTFSC	STATUS,Z
				CALL	TIM						;User selects times
				
				MOVFW	GPFILE0
				XORLW	02H						;User selects change code
				BTFSC	STATUS,Z
				CALL	CHGCODEA

				MOVFW	GPFILE0
				XORLW	03H						;User selects change code
				BTFSC	STATUS,Z
				CALL	CHGCODEB				
				
				MOVFW	GPFILE0
				XORLW	04H						;User selects return
				BTFSC	STATUS,Z
				GOTO	ESAVE
				GOTO	SYS
						
ESAVE			BTFSC	PORTB,HIDECHR			;Set flag if hide keys link is made
				BSF		SYSFLAGS_B,HIDEINKEYS
				CALL	WRFLSH					;Save to flash
				RETURN
				
;---------------|-------|-----------------------|------------------------------------------------
;System Timers Menu
;---------------|-------|-----------------------|------------------------------------------------				
TIM				CALL	CLRLCD	
				CALL	LCDOFF
				CALL	PRNTIMENU
				CALL	SCANKEYS
				MOVWF	GPFILE0
				XORLW	01H
				BTFSC	STATUS,Z
				CALL	ENTRYTIME
				MOVFW	GPFILE0
				XORLW	02H
				BTFSC	STATUS,Z
				CALL	EXITTIME
				MOVFW	GPFILE0
				XORLW	03H
				BTFSC	STATUS,Z
				CALL	ALARMTIME
				MOVFW	GPFILE0
				XORLW	04H
				BTFSC	STATUS,Z
				RETURN	
				GOTO	TIM
				
;---------------|-------|-----------------------|------------------------------------------------
;New Entry Time
;---------------|-------|-----------------------|------------------------------------------------				
ENTRYTIME		CALL	CLRLCD					;Clear Down Boot Logo
				CALL	LCDOFF
				CALL	CLRTMPARY
				CALL	LCDLINE1
				CALL	PRNNEW
				CALL	PRNENTRY
				CALL	PRNTIME
				CALL	LCDLINE4
				CALL	PRN0T99
				CALL	CLRTMPARY	
				CALL	READTOTMP				;Read password from keyboard
				
				MOVFW	TMP0					;Only change file if something was entered
				XORLW	020H
				BTFSC	STATUS,Z
				RETURN
				
				CALL	TMPTOHEX
				MOVWF	ENTTIME	
				RETURN
				
;---------------|-------|-----------------------|------------------------------------------------
;New Exit Time
;---------------|-------|-----------------------|------------------------------------------------					
EXITTIME		CALL	CLRLCD					;Clear Down Boot Logo
				CALL	LCDOFF
				CALL	CLRTMPARY
				CALL	LCDLINE1
				CALL	PRNNEW
				CALL	PRNEXIT
				CALL	PRNTIME
				CALL	LCDLINE4
				CALL	PRN0T99
				CALL	CLRTMPARY	
				CALL	READTOTMP				;Read password from keyboard
				
				MOVFW	TMP0					;Only change file if something was entered
				XORLW	020H
				BTFSC	STATUS,Z
				RETURN
				
				CALL	TMPTOHEX
				MOVWF	EXTTIME	
				RETURN
				
;---------------|-------|-----------------------|------------------------------------------------
;New Alarm Time
;---------------|-------|-----------------------|------------------------------------------------					
ALARMTIME		CALL	CLRLCD					;Clear Down Boot Logo
				CALL	LCDOFF
				CALL	CLRTMPARY
				CALL	LCDLINE1
				CALL	PRNNEW
				CALL	PRNALARM
				CALL	PRNTIME
				CALL	LCDLINE4
				CALL	PRN0T99
				CALL	CLRTMPARY	
				CALL	READTOTMP				;Read password from keyboard

				
				MOVFW	TMP0					;Only change file if something was entered
				XORLW	020H
				BTFSC	STATUS,Z
				RETURN
				
				CALL	TMPTOHEX
				MOVWF	ALMTIME	
				RETURN
				
;---------------|-------|-----------------------|------------------------------------------------
;Extract Hex from entered number
;---------------|-------|-----------------------|------------------------------------------------			
TMPTOHEX		CLRF	GPFILE1
				MOVFW	TMP1
				XORLW	020H
				BTFSC	STATUS,Z
				GOTO	ONEHEXCHAR
				
				MOVLW	030H
				SUBWF	TMP0,W
				MOVWF	GPFILE0
				
TWOHEXCHAR		MOVLW	0AH
				ADDWF	GPFILE1,F
				DECF	GPFILE0,F
				BTFSS	STATUS,Z
				GOTO	TWOHEXCHAR
				
				MOVLW	030H
				SUBWF	TMP1,W
				ADDWF	GPFILE1,W
				RETURN		

ONEHEXCHAR		MOVLW	030H
				SUBWF	TMP0,W
				ADDWF	GPFILE1,W
				RETURN		
				
;---------------|-------|-----------------------|------------------------------------------------
;System Codes Menu
;---------------|-------|-----------------------|------------------------------------------------
;COD			CALL	CLRLCD	
;				CALL	LCDOFF
;				CALL	PRNCOMENU
;				CALL	SCANKEYS
;				MOVWF	GPFILE0
;				XORLW	01H
;				BTFSC	STATUS,Z
;				CALL	CHGA
;				MOVFW	GPFILE0
;				XORLW	04H
;				BTFSS	STATUS,Z
;				GOTO	COD
;				RETURN
				
;CHGA			CALL	CHKCODEA				;check entry to see if it matches 1s valid code
;				XORLW	A'P'
;				BTFSC	STATUS,Z
;				CALL	SETCODEA				;allow user to enter new code
;				RETURN	
				
;---------------|-------|-----------------------|------------------------------------------------
;Enter code into A array
;---------------|-------|-----------------------|------------------------------------------------
SETCODEA		CALL	CLRLCD					;Clear Down Boot Logo
				CALL	LCDOFF
				CALL	CLRTMPARY
				CALL	LCDLINE1
				CALL	PRNENTER
				CALL	PRNNEW
				CALL	PRNCODE
				MOVLW	A'A' 
				CALL	DTOG
				CALL	READTOTMP				;Read password from keyboard
				CALL	CPYTMPA					;copy into 1st valid location	
				RETURN
				
;---------------|-------|-----------------------|------------------------------------------------
;Enter code into B array
;---------------|-------|-----------------------|------------------------------------------------
SETCODEB		CALL	CLRLCD					;Clear Down Boot Logo
				CALL	LCDOFF
				CALL	CLRTMPARY
				CALL	LCDLINE1
				CALL	PRNENTER
				CALL	PRNNEW
				CALL	PRNCODE
				MOVLW	A'B' 
				CALL	DTOG
				CALL	READTOTMP				;Read password from keyboard
				CALL	CPYTMPB					;copy into 1st valid location	
				RETURN
				
;---------------|-------|-----------------------|------------------------------------------------
;Enter valid code for code chge
;---------------|-------|-----------------------|------------------------------------------------					
CHGCODEA		CALL	CLRLCD					;Clear down screen
				CALL	LCDOFF					;Ask user to enter code
				CALL	CLRTMPARY
				CALL	LCDLINE1
				CALL	PRNENTER
				CALL	PRNSET
				CALL	PRNCODE
				MOVLW	A'A' 
				CALL	DTOG
				CALL	READTOTMP				;Read Keyboards into temporary array
				
				CALL	LCDLINE4				;***Remove***
				CALL	PRNCODE					;***Remove***
				
				CALL	CHKCODEA				;check entry to see if it matches 1s valid code
				XORLW	A'P'
				BTFSC	STATUS,Z
				CALL	SETCODEA
				
				RETURN		
				
;---------------|-------|-----------------------|------------------------------------------------
;Enter valid code for code chge
;---------------|-------|-----------------------|------------------------------------------------					
CHGCODEB		CALL	CLRLCD					;Clear down screen
				CALL	LCDOFF					;Ask user to enter code
				CALL	CLRTMPARY
				CALL	LCDLINE1
				CALL	PRNENTER
				CALL	PRNSET
				CALL	PRNCODE
				MOVLW	A'B' 
				CALL	DTOG
				CALL	READTOTMP				;Read Keyboards into temporary array
				
				CALL	LCDLINE4				;***Remove***
				CALL	PRNCODE					;***Remove***
				
				CALL	CHKCODEB				;check entry to see if it matches 1s valid code
				XORLW	A'P'
				BTFSC	STATUS,Z
				CALL	SETCODEB
				
				RETURN									
;***************|*******|***********************|************************************************	
;Alarm Subs
;***************|*******|***********************|************************************************
;---------------|-------|-----------------------|------------------------------------------------
;Set System Code Entry Screen
;---------------|-------|-----------------------|------------------------------------------------				
SETSYS			CALL	CLRLCD					;Clear down screen
				CALL	LCDOFF					;Ask user to enter code
				CALL	CLRTMPARY
				CALL	LCDLINE1
				CALL	PRNENTER
				CALL	PRNSET
				CALL	PRNCODE
				CALL	READTOTMP				;Read Keyboards into temporary array
				
;				CALL	LCDLINE4				;***Remove***
;				CALL	PRNCODE					;***Remove***
				
				CALL	CHKCODEA				;check entry to see if it matches 1s valid code
				XORLW	A'P'
				BTFSC	STATUS,Z
				GOTO	SYSSETTING				;if valid go to alarm setting
				
				CALL	CHKCODEB				;check entry to see if it matches 1s valid code
				XORLW	A'P'
				BTFSC	STATUS,Z
				GOTO	SYSSETTING				;if valid go to alarm setting
				
				RETURN
					
;---------------|-------|-----------------------|------------------------------------------------
;System Setting Screen
;---------------|-------|-----------------------|------------------------------------------------	
SYSSETTING		MOVFW	EXTTIME					;Set state and start timer
				MOVWF	EVENTSECCNTR
				CALL	EXITIMING

				CALL	CLRLCD					;Clear Down and print setting screen
				CALL	LCDOFF
				CALL	CLRTMPARY
				CALL	LCDLINE1
				MOVLW	A' ' 
				CALL	DTOG
				CALL	PRNSETTING
				CALL	PRNPRESS
				CALL	LCDLINE2
				MOVLW	A' ' 
				CALL	DTOG
				CALL	DTOG
				MOVLW	A'*' 
				CALL	DTOG
				MOVLW	A' ' 
				CALL	DTOG
				CALL	PRNTO
				CALL	PRNSTOP
				
HOLDSTART		BSF		PORTB,STATECHG			;hold state change lamp on
				MOVFW	EXTTIME					;Set state and start timer
				MOVWF	EVENTSECCNTR
				BCF		INTCON,T0IE				;Disable timer interrupt
				CALL	LCDLINE3
				CALL	PRNZONESTATES
			
SCANSTAR		MOVLW	03H						;Check for * being pressed
				MOVWF	PORTD
				BSF		PORTE,MUXEN
				MOVLW	01H
				BTFSS	PORTD,4					;if key row1,colum3 is pressed exit
				GOTO 	STOPSET
			
				BTFSC	PORTB,ZONE1				;Hold if zones are not clear
				GOTO	HOLDSTART
				BTFSC	PORTB,ZONE2
				GOTO	HOLDSTART
				
				BSF		INTCON,T0IE				;re able timer interrupt
				
				CALL	LCDLINE3
				CALL	PRNZONESTATES
			
				CALL	LCDLINE4
				MOVLW	A' ' 
				CALL	DTOG
				CALL	PRNSET
				CALL 	PRNIN
				CALL	PRTSECCNT
				MOVLW	A' ' 
				CALL	DTOG
				CALL	PRNSECS					

JMPLED			BTFSC	SYSFLAGS_A,ALARMSET		;Flash led until alarm set
				GOTO	SYSSET 

				GOTO	SCANSTAR

STOPSET			BCF		INTCON,T0IE				;Disable timer interrupt
				CALL	CLRLCD					;Clear Down Boot Logo
				CALL	LCDOFF
				RETURN

;---------------|-------|-----------------------|------------------------------------------------
;System Set
;---------------|-------|-----------------------|------------------------------------------------	
SYSSET			CALL	SYSTEMSET
				CALL	CLRLCD					;Clear Down and print STATECHG screen
				CALL	LCDOFF
				
				CALL	LCDLINE1
				
				CALL	LCDLINE2
				MOVLW	A' ' 
				CALL	DTOG
				CALL	DTOG
				CALL	DTOG
				CALL	PRNALARM
				CALL	PRNSET

				
SYSINSET		BCF		INTCON,T0IE				;Enable timer interrupt
				CALL	LCDLINE3
				CALL	PRNZONESTATES
				
				BTFSC	PORTB,ZONE1				;Start entry timer on zone 1 trigger
				GOTO	ENTRYDELAY
				BTFSC	PORTB,ZONE2				;Jump to alarm on zone 2 trigger
				GOTO	ALARMACTIVE
			
				GOTO	SYSINSET
				
;---------------|-------|-----------------------|------------------------------------------------
;Entry Delay
;---------------|-------|-----------------------|------------------------------------------------
ENTRYDELAY		MOVFW	ENTTIME					;Set state and start timer
				MOVWF	EVENTSECCNTR
				CALL	ENTRYTIMING
				
ETRYDELAY		CALL	CLRLCD					;Clear Down and print setting screen
				CALL	LCDOFF
				CALL	CLRTMPARY
				CALL	LCDLINE1
				CALL	PRNALARM
				CALL	PRNCOUNTDOWN

				CALL	LCDLINE2
				CALL	PRNTO
				CALL	PRNUNSET
				CALL	PRNPRESS
				MOVLW	A'*' 
				CALL	DTOG
			
ENTRYCOUNT		BTFSC	SYSFLAGS_A,ALARM		;Flash led until alarm set
				GOTO	ALARMACTIVE
				
				BTFSC	PORTB,ZONE2				;Jump to alarm on zone 2 trigger
				GOTO	ALARMACTIVE
				
				MOVLW	03H						;Check for * being pressed
				MOVWF	PORTD
				BSF		PORTE,MUXEN
				MOVLW	01H
				BTFSC	PORTD,4					;if key row1,colum3 is pressed exit
				GOTO	EMSTARSKIP
				CALL	UNSETSYS				;Read keyboard
				BTFSC	SYSFLAGS_A,ALARMUNSET 	;check for state change from alarm active to unset 
				RETURN							;if system  unset change
				BTFSC	SYSFLAGS_A,ALARM	 	;check for state change from alarm active to set 
				GOTO	ALARMACTIVE				;if system  set but not active change 	
				GOTO	ETRYDELAY

EMSTARSKIP		CALL	LCDLINE4
				CALL	PRNALARM
				CALL 	PRNIN
				CALL	PRTSECCNT
				MOVLW	A' ' 
				CALL	DTOG
				CALL	PRNSECS	
				
				BTFSC	SYSFLAGS_A,ALARMTIMING
				GOTO	ALARMACTIVE
				GOTO	ENTRYCOUNT

;---------------|-------|-----------------------|------------------------------------------------
;Alarm active
;---------------|-------|-----------------------|------------------------------------------------
ALARMACTIVE		MOVFW	ALMTIME					;Set state and start timer
				MOVWF	EVENTMINCNTR
				BTFSS	STATUS,Z				;Decrement as long as the time is not zero
				DECF	EVENTMINCNTR,F
				MOVLW	03BH						
				MOVWF	EVENTSECCNTR
				
ALMACTIVE		CALL	SYSTEMALARM
				
				CALL	CLRLCD					;Clear Down and print SETTING screen
				CALL	LCDOFF
				CALL	CLRTMPARY
				CALL	LCDLINE1
				MOVLW	A' ' 
				CALL	DTOG
				CALL	DTOG
				CALL	PRNALARM
				MOVLW	A' ' 
				CALL	DTOG
				CALL	PRNALARM

				CALL	LCDLINE2
				CALL	PRNTO
				CALL	PRNUNSET
				CALL	PRNPRESS
				MOVLW	A'*' 
				CALL	DTOG				
				

ALARMCOUNT		CALL	LCDLINE4
				MOVLW	A' ' 
				CALL	DTOG
				CALL	DTOG
				CALL	DTOG
				CALL	DTOG
				CALL	DTOG
				CALL	DTOG
				CALL	PRTMINCNT
				MOVLW	A':' 
				CALL	DTOG
				CALL	PRTSECCNT
				
				MOVLW	03H						;Check for * being pressed
				MOVWF	PORTD
				BSF		PORTE,MUXEN
				MOVLW	01H
				BTFSC	PORTD,4					;if key row1,colum3 is pressed exit
				GOTO	AMSTARSKIP
				CALL	UNSETSYS
				BTFSC	SYSFLAGS_A,ALARMUNSET 
				RETURN
				
				BTFSC	SYSFLAGS_A,ALARMSET 	;check for state change from alarm active to set 
				GOTO	SYSSET					;if system  set but not active change 
				GOTO	ALMACTIVE
				
AMSTARSKIP		BTFSC	SYSFLAGS_A,ALARMSET 
				GOTO	SYSSET
				GOTO	ALARMCOUNT
				
;---------------|-------|-----------------------|------------------------------------------------
;Set Alarm States and Timing functions
;---------------|-------|-----------------------|------------------------------------------------					
UNSETSYS		CALL	CLRLCD					;Clear down screen
				CALL	LCDOFF					;Ask user to enter code
				CALL	CLRTMPARY
				CALL	LCDLINE1
				CALL	PRNENTER
				CALL	PRNUNSET
				CALL	PRNCODE
				CALL	READTOTMP				;Read Keyboards into temporary array
				
;				CALL	LCDLINE4				;***Remove***
;				CALL	PRNCODE					;***Remove***
				
				CALL	CHKCODEA				;check entry to see if it matches 1s valid code
				XORLW	A'P'
				BTFSC	STATUS,Z
				CALL	SYSTEMUNSET	
				
				CALL	CHKCODEB				;check entry to see if it matches 1s valid code
				XORLW	A'P'
				BTFSC	STATUS,Z
				CALL	SYSTEMUNSET	
				
				RETURN
				
;***************|*******|***********************|************************************************	
;Alarm States and Timing activation/deactivation functions
;***************|*******|***********************|************************************************							
SYSTEMUNSET		BSF		SYSFLAGS_A,ALARMUNSET
				BCF		SYSFLAGS_A,ALARMSETTING
				BCF		SYSFLAGS_A,ALARMSET 
				BCF 	SYSFLAGS_A,ALARMENTRY		
				BCF 	SYSFLAGS_A,ALARMTIMING	
				
				BCF		INTCON,T0IE				;Stop the clock	
				
				BSF		PORTB,UNSET				;Set alarm status lamps
				BCF		PORTB,STATECHG
				BCF		PORTB,ALMSET
				BCF		PORTB,ALARM
				RETURN	
				
EXITIMING		BCF		SYSFLAGS_A,ALARMUNSET
				BSF		SYSFLAGS_A,ALARMSETTING
				BCF		SYSFLAGS_A,ALARMSET 
				BCF 	SYSFLAGS_A,ALARMENTRY		
				BCF 	SYSFLAGS_A,ALARMTIMING
				BSF		INTCON,T0IE				;Enable timer interrupt
				
				BCF		PORTB,UNSET				;Set alarm status lamps
				BCF		PORTB,STATECHG
				BCF		PORTB,ALMSET
				BCF		PORTB,ALARM
				RETURN

SYSTEMSET		BCF		SYSFLAGS_A,ALARMUNSET
				BCF		SYSFLAGS_A,ALARMSETTING
				BSF		SYSFLAGS_A,ALARMSET 
				BCF 	SYSFLAGS_A,ALARMENTRY		
				BCF 	SYSFLAGS_A,ALARMTIMING	
				BCF		INTCON,T0IE				;Stop the clock	
				BSF		SYSFLAGS_A,KEYABORT		;Force Key entry code to end.
				
				BCF		PORTB,UNSET				;Set alarm status lamps
				BCF		PORTB,STATECHG
				BSF		PORTB,ALMSET
				BCF		PORTB,ALARM
				
				RETURN				

ENTRYTIMING		BCF		SYSFLAGS_A,ALARMUNSET
				BCF		SYSFLAGS_A,ALARMSETTING
				BCF		SYSFLAGS_A,ALARMSET 
				BSF 	SYSFLAGS_A,ALARMENTRY		
				BCF 	SYSFLAGS_A,ALARMTIMING					
				BSF		INTCON,T0IE				;Enable timer interrupt
				
				BCF		PORTB,UNSET				;Set alarm status lamps
				BCF		PORTB,STATECHG
				BSF		PORTB,ALMSET
				BCF		PORTB,ALARM
				RETURN
				
SYSTEMALARM		BCF		SYSFLAGS_A,ALARMUNSET
				BCF		SYSFLAGS_A,ALARMSETTING
				BCF		SYSFLAGS_A,ALARMSET 
				BCF 	SYSFLAGS_A,ALARMENTRY		
				BSF 	SYSFLAGS_A,ALARMTIMING	
				BSF		INTCON,T0IE				;Enable timer interrupt
				BSF		SYSFLAGS_A,KEYABORT	;Force Key entry code to end.
				
				BCF		PORTB,UNSET				;Set alarm status lamps
				BCF		PORTB,STATECHG
				BSF		PORTB,ALMSET
				BSF		PORTB,ALARM
				
				RETURN
				
;***************|*******|***********************|************************************************	
;Hardware Services/Supporting Routines
;***************|*******|***********************|************************************************
;---------------|-------|-----------------------|------------------------------------------------
;LCD Services
;---------------|-------|-----------------------|------------------------------------------------
INTDSP			BCF		PORTE,EN				;
				BCF		PORTE,RS

				MOVLW   0ffH        			;LOAD FF INTO THE W REG
				CALL	VDELAY					;WAIT FOR POWER TO COME UP TO +4.5V
				MOVLW	03H						;Power On Sequence
				CALL	ITOGE
				MOVLW   0ffH        			;LOAD FF INTO THE W REG
				CALL	VDELAY	
				MOVLW	03H	
				CALL	ITOGE
				CALL	ITOGE
				MOVLW 	02H						;4 Bit Mode	
				CALL	ITOGE
								
				MOVLW 	028H					;Function Set
				CALL	ITOG
				MOVFW	08H						;Display Off, Cursor Off, Blink Off
				CALL	ITOG  
				MOVFW	01H						;Clear Screen & Return Cursor Home
				CALL	ITOG
				MOVLW	06H						;inc cursor to right and 
				CALL	ITOG					;don't move screen when writing
				MOVLW	0CH						;Turn Display On
				CALL	ITOG
				
				RETURN							;end initialisation 
	
;-------THIS ROUTINE TOGGLS THE LCDS ENABLE PIN------------
ITOGE			MOVWF		TMPDATA
				MOVWF		PORTD	 			;TRANSFER W TO DATA PORT
				CALL		QDELAY
				BSF			PORTE,EN			;THE ENABLE LINE IS SET
				BCF			PORTE,EN	
				CLRF		PORTD				;THE PORT IS CLEARED
				MOVF		TMPDATA,W			;RESTORE DATA
				RETURN							;END OF ROUTINE
	
;-------THIS ROUTINE TOGGLS THE LCDS ENABLE PIN------------
ITOG			MOVWF		TMPDATA
				BCF			PORTE,RS			;RS LINE CLEARED COMMAND
				GOTO		TOG
				
DTOG			MOVWF		TMPDATA
				BSF			PORTE,RS			;RS LINE SET DATA

TOG				BCF		PORTE,MUXEN
				CALL		QDELAY	
			
				;SEND UPPER NIBBLE
				BSF			PORTE,EN			;THE ENABLE LINE IS SET
				MOVF		TMPDATA,W			;DATA IS PRESENTED TO PORTB
				ANDLW		B'11110000'			;MASK OFF LOWER NIBBLE
				MOVWF		WKFILE				;MOVE DATA FOR ROTATION
				SWAPF		WKFILE,W			;MOVE MASK RESULT TO GENERAL REG
				MOVWF		PORTD
				BCF			PORTE,EN			;
				
				;SEND LOWER NIBBLE	
				BSF			PORTE,EN			;THE ENABLE LINE IS SET
				MOVF		TMPDATA,W			;DATA IS PRESENTED TO PORTB
				ANDLW		B'00001111'			;MASK OFF LOWER NIBBLE
				MOVWF		PORTD
				BCF			PORTE,EN			;
				MOVF		TMPDATA,W
				CLRF		PORTD				;THE PORT IS CLEARED
				
				RETURN							;END OF ROUTINE

;---------------|-------|-----------------------|------------------------------------------------
LCDOFF			MOVLW	08H              	
				CALL	ITOG
				RETURN
				
;---------------|-------|-----------------------|------------------------------------------------
CLRLCD			MOVLW	01H              	
				CALL	ITOG
				RETURN
				
;---------------|-------|-----------------------|------------------------------------------------
CURSON			MOVLW	0EH              	
				CALL	ITOG
				RETURN

;---------------|-------|-----------------------|------------------------------------------------
LCDLINE1		MOVLW	080H              	
				CALL 	ITOG
				RETURN

;---------------|-------|-----------------------|------------------------------------------------
LCDLINE2		MOVLW	0C0H              	
				CALL 	ITOG
				RETURN

;---------------|-------|-----------------------|------------------------------------------------
LCDLINE3		MOVLW	090H              	    
				CALL	ITOG
				RETURN

;---------------|-------|-----------------------|------------------------------------------------
LCDLINE4		MOVLW	0D0H              	    
				CALL	ITOG
				RETURN

;---------------|-------|-----------------------|------------------------------------------------
BOOTLOGO		CALL 	LCDLINE1				;
		
				MOVLW	A' ' 
				CALL	DTOG 
				CALL	DTOG
				MOVLW	A'C' 
				CALL	DTOG
				MOVLW	A'o' 
				CALL	DTOG
				MOVLW	A'd'
				CALL	DTOG
				MOVLW	A'e' 
				CALL	DTOG
				MOVLW	A'd'
				CALL	DTOG				
				MOVLW	A' ' 
				CALL	DTOG
				MOVLW	A'A'
				CALL	DTOG
				MOVLW	A'l' 
				CALL	DTOG
				MOVLW	A'a'
				CALL	DTOG	
				MOVLW	A'r'
				CALL	DTOG
				MOVLW	A'm'
				CALL	DTOG
				
				CALL 	LCDLINE2				;
				MOVLW	A' ' 
				CALL	DTOG
				CALL	DTOG
				CALL	DTOG
				CALL	DTOG
				CALL	DTOG
				MOVLW	A'G' 
				CALL	DTOG
				MOVLW	A'W' 
				CALL	DTOG
				MOVLW	A'7'
				CALL	DTOG
				MOVLW	A'M' 
				CALL	DTOG
				MOVLW	A'Q' 
				CALL	DTOG
				MOVLW	A'E' 
				CALL	DTOG

				CALL 	LCDLINE3				;
				MOVLW	A' '				
				CALL	DTOG
				CALL	DTOG
				CALL	DTOG
				CALL	DTOG
				MOVLW	A'2' 
				CALL	DTOG
				MOVLW	A'0' 
				CALL	DTOG
				MOVLW	A'1'
				CALL	DTOG
				MOVLW	A'4' 
				CALL	DTOG
				MOVLW	A'0' 
				CALL	DTOG
				MOVLW	A'4' 
				CALL	DTOG
				MOVLW	A'1' 
				CALL	DTOG
				MOVLW	A'0' 
				CALL	DTOG
				
				CALL 	LCDLINE4				;
				MOVLW	A' ' 
				CALL	DTOG
				CALL	DTOG
				CALL	DTOG
				CALL	DTOG
				CALL	DTOG
				MOVLW	A'v' 
				CALL	DTOG
				MOVLW	A'0'
				CALL	DTOG
				MOVLW	A'0' 
				CALL	DTOG
				MOVLW	A'.' 
				CALL	DTOG
				MOVLW	A'1' 
				CALL	DTOG
				MOVLW	A'4' 
				CALL	DTOG
				
				RETURN
				
;---------------|-------|-----------------------|------------------------------------------------	
CPYTMPA			MOVFW	TMP0
				MOVWF	PASSA0
				MOVFW	TMP1
				MOVWF	PASSA1
				MOVFW	TMP2
				MOVWF	PASSA2
				MOVFW	TMP3
				MOVWF	PASSA3
				MOVFW	TMP4
				MOVWF	PASSA4
				MOVFW	TMP5
				MOVWF	PASSA5
				MOVFW	TMP6
				MOVWF	PASSA6
				MOVFW	TMP7
				MOVWF	PASSA7
				RETURN
				
;---------------|-------|-----------------------|------------------------------------------------	
CPYTMPB			MOVFW	TMP0
				MOVWF	PASSB0
				MOVFW	TMP1
				MOVWF	PASSB1
				MOVFW	TMP2
				MOVWF	PASSB2
				MOVFW	TMP3
				MOVWF	PASSB3
				MOVFW	TMP4
				MOVWF	PASSB4
				MOVFW	TMP5
				MOVWF	PASSB5
				MOVFW	TMP6
				MOVWF	PASSB6
				MOVFW	TMP7
				MOVWF	PASSB7
				RETURN				
				
;---------------|-------|-----------------------|------------------------------------------------	
CHKCODEA		MOVFW	TMP0
				XORWF	PASSA0,W
				BTFSS 	STATUS,Z
				GOTO	CHKFAILA
				
				MOVFW	TMP1
				XORWF	PASSA1,W
				BTFSS 	STATUS,Z
				GOTO	CHKFAILA
				
				MOVFW	TMP2
				XORWF	PASSA2,W
				BTFSS 	STATUS,Z
				GOTO	CHKFAILA
				
				MOVFW	TMP3
				XORWF	PASSA3,W
				BTFSS 	STATUS,Z
				GOTO	CHKFAILA
				
				MOVFW	TMP4
				XORWF	PASSA4,W
				BTFSS 	STATUS,Z
				GOTO	CHKFAILA
				
				MOVFW	TMP5
				XORWF	PASSA5,W
				BTFSS 	STATUS,Z
				GOTO	CHKFAILA
				
				MOVFW	TMP6
				XORWF	PASSA6,W
				BTFSS 	STATUS,Z
				GOTO	CHKFAILA
				
				MOVFW	TMP7
				XORWF	PASSA7,W
				BTFSS 	STATUS,Z
				GOTO	CHKFAILA
	
CHKPASSA		MOVLW	A'P'	
				RETURN
CHKFAILA		MOVLW	A'F'
				RETURN

;---------------|-------|-----------------------|------------------------------------------------	
CHKCODEB		MOVFW	TMP0
				XORWF	PASSB0,W
				BTFSS 	STATUS,Z
				GOTO	CHKFAILB
				
				MOVFW	TMP1
				XORWF	PASSB1,W
				BTFSS 	STATUS,Z
				GOTO	CHKFAILB
				
				MOVFW	TMP2
				XORWF	PASSB2,W
				BTFSS 	STATUS,Z
				GOTO	CHKFAILB
				
				MOVFW	TMP3
				XORWF	PASSB3,W
				BTFSS 	STATUS,Z
				GOTO	CHKFAILB
				
				MOVFW	TMP4
				XORWF	PASSB4,W
				BTFSS 	STATUS,Z
				GOTO	CHKFAILB
				
				MOVFW	TMP5
				XORWF	PASSB5,W
				BTFSS 	STATUS,Z
				GOTO	CHKFAILB
				
				MOVFW	TMP6
				XORWF	PASSB6,W
				BTFSS 	STATUS,Z
				GOTO	CHKFAILB
				
				MOVFW	TMP7
				XORWF	PASSB7,W
				BTFSS 	STATUS,Z
				GOTO	CHKFAILB
	
CHKPASSB		MOVLW	A'P'	
				RETURN
CHKFAILB		MOVLW	A'F'
				RETURN	
								
;---------------|-------|-----------------------|------------------------------------------------				
PRNZONESTATES	CALL	PRNZONE
				MOVLW	A'1'
				CALL	DTOG
				MOVLW	A':'
				CALL	DTOG
				MOVLW	A'0'
				BTFSC	PORTB,ZONE1
				MOVLW	A'1'
				CALL	DTOG
				MOVLW	A' '
				CALL	DTOG
				CALL	DTOG
				CALL	PRNZONE
				MOVLW	A'2'
				CALL	DTOG
				MOVLW	A':'
				CALL	DTOG
				MOVLW	A'0'
				BTFSC	PORTB,ZONE2
				MOVLW	A'1'
				CALL	DTOG	
				RETURN
				
;---------------|-------|-----------------------|------------------------------------------------				
PRTSECCNT		MOVFW	EVENTSECCNTR
				MOVWF	HEXBT
				CALL	HDEC
				
				;MOVFW	EHUNS
				;ADDLW	030H
				;CALL	DTOG
				MOVFW	ETENS
				ADDLW	030H
				CALL	DTOG
				MOVFW	EUNIT
				ADDLW	030H
				CALL	DTOG
				RETURN

;---------------|-------|-----------------------|------------------------------------------------				
PRTMINCNT		MOVFW	EVENTMINCNTR
				MOVWF	HEXBT
				CALL	HDEC
				
				MOVFW	ETENS
				ADDLW	030H
				CALL	DTOG
				MOVFW	EUNIT
				ADDLW	030H
				CALL	DTOG
				RETURN	
				
;---------------|-------|-----------------------|------------------------------------------------				
PRNEXTTIME		MOVWF	HEXBT
				CALL	HDEC
				MOVFW	ETENS
				ADDLW	030H
				CALL	DTOG
				MOVFW	EUNIT
				ADDLW	030H
				CALL	DTOG
				RETURN				
				
;---------------|-------|-----------------------|------------------------------------------------
PRNMNMENU		CALL 	CLRLCD
				CALL	LCDOFF
				CALL	LCDLINE1
				CALL	PRNMAIN
				CALL	PRNMENU
				CALL	LCDLINE2
				MOVLW	A'1' 
				CALL	DTOG
				MOVLW	A'.' 
				CALL	DTOG
				CALL	PRNSET
				CALL	PRNALARM
				CALL	LCDLINE3
				MOVLW	A'2' 
				CALL	DTOG
				MOVLW	A'.' 
				CALL	DTOG
				CALL	PRNPRESETS
				RETURN
				
;---------------|-------|-----------------------|------------------------------------------------
PRNSYMENU		CALL 	CLRLCD
				CALL	LCDOFF
				CALL	LCDLINE1
				CALL	PRNPRESETS
				
				MOVLW	A'*' 
				CALL	DTOG
				MOVLW	A'-' 
				CALL	DTOG
				CALL	PRNRETURN
				
				CALL	LCDLINE2
				MOVLW	A'1' 
				CALL	DTOG
				MOVLW	A'.' 
				CALL	DTOG
				CALL	PRNCHANGE
				CALL	PRNTIMERS
				
				CALL	LCDLINE3
				MOVLW	A'2' 
				CALL	DTOG
				MOVLW	A'.' 
				CALL	DTOG
				CALL	PRNCHANGE
				CALL	PRNCODE
				MOVLW	A'A' 
				CALL	DTOG
				
				CALL	LCDLINE4
				MOVLW	A'3' 
				CALL	DTOG
				MOVLW	A'.' 
				CALL	DTOG
				CALL	PRNCHANGE
				CALL	PRNCODE
				MOVLW	A'B' 
				CALL	DTOG
				
				RETURN
				
;---------------|-------|-----------------------|------------------------------------------------
PRNCOMENU		CALL 	CLRLCD
				CALL	LCDOFF
				CALL	LCDLINE1
				CALL	PRNCODE
				
				MOVLW	A' ' 
				CALL	DTOG
				CALL	DTOG
				CALL	DTOG
				MOVLW	A'*' 
				CALL	DTOG
				MOVLW	A'-' 
				CALL	DTOG
				CALL	PRNRETURN
				
				CALL	LCDLINE2
				MOVLW	A'1' 
				CALL	DTOG
				MOVLW	A'.' 
				CALL	DTOG
				CALL	PRNCHANGE
				CALL	PRNCODE				
				MOVLW	A'A' 
				CALL	DTOG
				RETURN	
				
;---------------|-------|-----------------------|------------------------------------------------
PRNTIMENU		CALL 	CLRLCD
				CALL	LCDOFF
				CALL	LCDLINE1
				CALL	PRNTIMERS
				
				MOVLW	A' ' 
				CALL	DTOG
				MOVLW	A'*' 
				CALL	DTOG
				MOVLW	A'-' 
				CALL	DTOG
				CALL	PRNRETURN

				CALL	LCDLINE2
				MOVLW	A'1' 
				CALL	DTOG
				MOVLW	A'.' 
				CALL	DTOG
				CALL	PRNENTRY
				CALL	PRNTIME
				MOVFW	ENTTIME
				CALL	PRNEXTTIME 
				MOVLW	A's' 
				CALL	DTOG
				
				CALL	LCDLINE3
				MOVLW	A'2' 
				CALL	DTOG
				MOVLW	A'.' 
				CALL	DTOG
				CALL	PRNEXIT
				CALL	PRNTIME
				MOVLW	A' ' 
				CALL	DTOG
				MOVFW	EXTTIME
				CALL	PRNEXTTIME 
				MOVLW	A's' 
				CALL	DTOG
				
				CALL	LCDLINE4
				MOVLW	A'3' 
				CALL	DTOG
				MOVLW	A'.' 
				CALL	DTOG
				CALL	PRNALARM
				CALL	PRNTIME
				MOVFW	ALMTIME
				CALL	PRNEXTTIME
				MOVLW	A'm' 
				CALL	DTOG
				RETURN	
				
;---------------|-------|-----------------------|------------------------------------------------
;Pre-defined print words				
;---------------|-------|-----------------------|------------------------------------------------
PRNSET			MOVLW	A'S'
				CALL	DTOG
				MOVLW	A'e'
				CALL	DTOG
				MOVLW	A't'
				CALL	DTOG
				MOVLW	A' ' 
				CALL	DTOG
				RETURN
				
PRNALARM		MOVLW	A'A'
				CALL	DTOG
				MOVLW	A'l'
				CALL	DTOG
				MOVLW	A'a'
				CALL	DTOG
				MOVLW	A'r' 
				CALL	DTOG
				MOVLW	A'm' 
				CALL	DTOG
				MOVLW	A' ' 
				CALL	DTOG
				RETURN
				
PRNCHANGE		MOVLW	A'C'
				CALL	DTOG
				MOVLW	A'h'
				CALL	DTOG
				MOVLW	A'a'
				CALL	DTOG
				MOVLW	A'n' 
				CALL	DTOG
				MOVLW	A'g' 
				CALL	DTOG
				MOVLW	A'e' 
				CALL	DTOG
				MOVLW	A' ' 
				CALL	DTOG
				RETURN

PRNTIMERS		MOVLW	A'T'
				CALL	DTOG
				MOVLW	A'i'
				CALL	DTOG
				MOVLW	A'm'
				CALL	DTOG
				MOVLW	A'e' 
				CALL	DTOG
				MOVLW	A'r' 
				CALL	DTOG
				MOVLW	A's' 
				CALL	DTOG
				MOVLW	A' ' 
				CALL	DTOG
				RETURN				

PRNMAIN			MOVLW	A'M'
				CALL	DTOG
				MOVLW	A'a'
				CALL	DTOG
				MOVLW	A'i'
				CALL	DTOG
				MOVLW	A'n' 
				CALL	DTOG
				MOVLW	A' ' 
				CALL	DTOG
				RETURN
				
PRNMENU			MOVLW	A'M'
				CALL	DTOG
				MOVLW	A'e'
				CALL	DTOG
				MOVLW	A'n'
				CALL	DTOG
				MOVLW	A'u' 
				CALL	DTOG
				MOVLW	A' ' 
				CALL	DTOG
				RETURN

PRNENTER		MOVLW	A'E'
				CALL	DTOG
				MOVLW	A'n'
				CALL	DTOG
				MOVLW	A't'
				CALL	DTOG
				MOVLW	A'e' 
				CALL	DTOG
				MOVLW	A'r' 
				CALL	DTOG
				MOVLW	A' ' 
				CALL	DTOG
				RETURN

PRNNEW			MOVLW	A'N' 
				CALL	DTOG
				MOVLW	A'e' 
				CALL	DTOG
				MOVLW	A'w' 
				CALL	DTOG
				MOVLW	A' ' 
				CALL	DTOG
				RETURN				
		
PRNCODE			MOVLW	A'C' 
				CALL	DTOG
				MOVLW	A'o' 
				CALL	DTOG
				MOVLW	A'd' 
				CALL	DTOG
				MOVLW	A'e' 
				CALL	DTOG
				MOVLW	A' ' 
				CALL	DTOG
				RETURN
				
PRNDONE			MOVLW	A'D' 
				CALL	DTOG
				MOVLW	A'o' 
				CALL	DTOG
				MOVLW	A'n' 
				CALL	DTOG
				MOVLW	A'e' 
				CALL	DTOG
				MOVLW	A' ' 
				CALL	DTOG
				RETURN			

PRNPRESETS		MOVLW	A'P' 
				CALL	DTOG
				MOVLW	A'r' 
				CALL	DTOG
				MOVLW	A'e' 
				CALL	DTOG
				MOVLW	A's' 
				CALL	DTOG
				MOVLW	A'e' 
				CALL	DTOG
				MOVLW	A't' 
				CALL	DTOG
				MOVLW	A's' 
				CALL	DTOG
				MOVLW	A' ' 
				CALL	DTOG
				RETURN			

PRNSETTING		MOVLW	A'S' 
				CALL	DTOG
				MOVLW	A'e' 
				CALL	DTOG
				MOVLW	A't' 
				CALL	DTOG
				MOVLW	A't' 
				CALL	DTOG
				MOVLW	A'i' 
				CALL	DTOG
				MOVLW	A'n' 
				CALL	DTOG
				MOVLW	A'g' 
				CALL	DTOG
				MOVLW	A' ' 
				CALL	DTOG
				RETURN				

				
PRNRETURN		MOVLW	A'R' 
				CALL	DTOG
				MOVLW	A'e' 
				CALL	DTOG
				MOVLW	A't' 
				CALL	DTOG
				MOVLW	A'u' 
				CALL	DTOG
				MOVLW	A'r' 
				CALL	DTOG
				MOVLW	A'n'  
				CALL	DTOG
				MOVLW	A' ' 
				CALL	DTOG
				RETURN						
				
PRNTIME			MOVLW	A'T' 
				CALL	DTOG
				MOVLW	A'i' 
				CALL	DTOG
				MOVLW	A'm' 
				CALL	DTOG
				MOVLW	A'e' 
				CALL	DTOG
				MOVLW	A' ' 
				CALL	DTOG
				RETURN	
				
PRNENTRY		MOVLW	A'E' 
				CALL	DTOG
				MOVLW	A'n' 
				CALL	DTOG
				MOVLW	A't' 
				CALL	DTOG
				MOVLW	A'r' 
				CALL	DTOG
				MOVLW	A'y' 
				CALL	DTOG
				MOVLW	A' ' 
				CALL	DTOG
				RETURN		

PRNPRESS		MOVLW	A'P' 
				CALL	DTOG
				MOVLW	A'r' 
				CALL	DTOG
				MOVLW	A'e' 
				CALL	DTOG
				MOVLW	A's' 
				CALL	DTOG
				MOVLW	A's' 
				CALL	DTOG
				MOVLW	A' ' 
				CALL	DTOG
				RETURN	
				
PRNTO			MOVLW	A'T' 
				CALL	DTOG
				MOVLW	A'o' 
				CALL	DTOG
				MOVLW	A' ' 
				CALL	DTOG
				RETURN	
				
PRNEXIT			MOVLW	A'E' 
				CALL	DTOG
				MOVLW	A'x' 
				CALL	DTOG
				MOVLW	A'i' 
				CALL	DTOG
				MOVLW	A't' 
				CALL	DTOG
				MOVLW	A' ' 
				CALL	DTOG
				RETURN		

PRNSTOP			MOVLW	A'S' 
				CALL	DTOG
				MOVLW	A't' 
				CALL	DTOG
				MOVLW	A'o' 
				CALL	DTOG
				MOVLW	A'p' 
				CALL	DTOG
				MOVLW	A' ' 
				CALL	DTOG
				RETURN	
				
				
PRNPASS			MOVLW	A'P' 
				CALL	DTOG
				MOVLW	A'a' 
				CALL	DTOG
				MOVLW	A's' 
				CALL	DTOG
				MOVLW	A's' 
				CALL	DTOG
				MOVLW	A' ' 
				CALL	DTOG
				RETURN	

PRNFAIL			MOVLW	A'F' 
				CALL	DTOG
				MOVLW	A'a' 
				CALL	DTOG
				MOVLW	A'i' 
				CALL	DTOG
				MOVLW	A'l' 
				CALL	DTOG
				MOVLW	A' ' 
				CALL	DTOG
				RETURN				
			
PRN0T99			MOVLW	A'0' 
				CALL	DTOG
				MOVLW	A' ' 
				CALL	DTOG
				MOVLW	A't' 
				CALL	DTOG
				MOVLW	A'o' 
				CALL	DTOG
				MOVLW	A' ' 
				CALL	DTOG
				MOVLW	A'9' 
				CALL	DTOG
				MOVLW	A'9' 
				CALL	DTOG
				MOVLW	A' ' 
				CALL	DTOG
				RETURN
				
PRNZONE			MOVLW	A'Z' 
				CALL	DTOG
				MOVLW	A'o' 
				CALL	DTOG
				MOVLW	A'n' 
				CALL	DTOG
				MOVLW	A'e' 
				CALL	DTOG 	
				RETURN
				
PRNUNSET		MOVLW	A'U' 
				CALL	DTOG
				MOVLW	A'n' 
				CALL	DTOG
				MOVLW	A's' 
				CALL	DTOG
				MOVLW	A'e' 
				CALL	DTOG
				MOVLW	A't' 
				CALL	DTOG
				MOVLW	A' ' 
				CALL	DTOG 
				RETURN		
				
				
PRNCOUNTDOWN	MOVLW	A'C' 
				CALL	DTOG
				MOVLW	A'o' 
				CALL	DTOG
				MOVLW	A'u' 
				CALL	DTOG
				MOVLW	A'n' 
				CALL	DTOG
				MOVLW	A't' 
				CALL	DTOG
				MOVLW	A'd' 
				CALL	DTOG
				MOVLW	A'o' 
				CALL	DTOG
				MOVLW	A'w' 
				CALL	DTOG
				MOVLW	A'n' 
				CALL	DTOG
				MOVLW	A' ' 
				CALL	DTOG 
				RETURN			
				
PRNIN			MOVLW	A'I' 
				CALL	DTOG
				MOVLW	A'n' 
				CALL	DTOG
				MOVLW	A' ' 
				CALL	DTOG 
				RETURN		

PRNSECS			MOVLW	A'S' 
				CALL	DTOG
				MOVLW	A'e' 
				CALL	DTOG
				MOVLW	A'c' 
				CALL	DTOG
				MOVLW	A's' 
				CALL	DTOG
				MOVLW	A' ' 
				CALL	DTOG 
				RETURN		
				
				
;---------------|-------|-----------------------|------------------------------------------------
;Scan keys and place result into working
;---------------|-------|-----------------------|------------------------------------------------				
SCANKEYS		MOVLW	00H
				MOVWF	PORTD
				BSF		PORTE,MUXEN
				
K1				MOVLW	01H
				BTFSC	PORTD,4					;
				GOTO	K2
K1W				BTFSC	PORTD,4	
				RETURN	
				GOTO	K1W
				
K2				MOVLW	02H
				BTFSC	PORTD,5					;
				GOTO	K3
K2W				BTFSC	PORTD,5
				RETURN	
				GOTO	K2W
				
K3				MOVLW	03H
				BTFSC	PORTD,6					;
				GOTO	KSTAR
K3W				BTFSC	PORTD,6
				RETURN	
				GOTO	K3W
				
KSTAR			MOVLW	03H
				MOVWF	PORTD
				BSF		PORTE,MUXEN

				MOVLW	04H
				BTFSC	PORTD,4					;
				GOTO	KN
KSTARW			BTFSC	PORTD,4
				RETURN	
				GOTO	KSTARW

				
KN				GOTO	SCANKEYS
				RETURN
				
;---------------|-------|-----------------------|------------------------------------------------
;Read Multiplexed Keyboard into a small temporary array
;---------------|-------|-----------------------|------------------------------------------------
READTOTMP		BCF		SYSFLAGS_A,KEYABORT
				BCF		SYSFLAGS_A,KEYFIN
				CLRF	WRITEPOINTER
RESCAN			CLRF	KEYROW					;clear row count
				
RESCANKEYS		MOVFW	KEYROW					;pre-set MUX
				MOVWF	PORTD
				BSF		PORTE,MUXEN
				CALL	READROWS				;scan the rows for a button press
				MOVFW	KEYROW
				MOVWF	PORTD
				BSF		PORTE,MUXEN
				INCF	KEYROW,F				;increment rows count
				BTFSC	KEYROW,2				;reset row when row count = 4
				GOTO	RESCAN
				
				BTFSC	SYSFLAGS_A,KEYABORT
				GOTO	ABORTSCAN
				BTFSC	SYSFLAGS_A,KEYFIN		;end key scan
				RETURN
				GOTO	RESCANKEYS
				
ABORTSCAN		CALL	CLRTMPARY				;Dump array if external process ends entry
				RETURN
				
;---------------|-------|-----------------------|------------------------------------------------				
READROWS		CLRF	KEYCOL					;clear columns count
;---------------|-------|-----------------------|------------------------------------------------

COL1			BTFSC	PORTD,4					;if no keys on the first row are being
				GOTO	COL2					;pushed then jump to next column check
				CALL	QDELAY
				BTFSC	PORTD,4					;de-bounce key press	
				GOTO	COL2
				
				MOVFW	KEYROW					;Check if row is 3 for * test
				SUBLW	03H
				BTFSS	STATUS,Z				;If Row3 and Col1 then * has been pressed
				GOTO	NOTSTAR					;Jump if not a star
				
												;check if array is full
				MOVFW	TMP7					;Check if on 8th char and del required
				XORLW	A' '
				BTFSC	STATUS,Z
				GOTO	NOT8TH
				MOVLW	A' '					;Clear location in array
				MOVWF	WRITECHAR
				CALL	WRITETMP
				CALL 	LCDLINE2
				CALL	PRNTMPARY
				GOTO	SKIPSTAR
				
												;* is being pressed and array is not full
NOT8TH			MOVFW	WRITEPOINTER			;Check pointer is not zero
				BTFSS	STATUS,Z
				DECF	WRITEPOINTER,F
				MOVLW	A' '					;Clear location in array
				MOVWF	WRITECHAR
				CALL	WRITETMP
				CALL 	LCDLINE2
				CALL	PRNTMPARY
				GOTO	SKIPSTAR				;Skip further tests
				
;---------------|-------|-----------------------|------------------------------------------------	
												;start normal key scanning
NOTSTAR			MOVFW	TMP7					;Abort array addition if array full
				XORLW	A' '
				BTFSS	STATUS,Z
				RETURN

				MOVFW	KEYROW					;form address into table
				MOVWF	TMPDATA
				BCF     STATUS,C				;clear carry before rotation
				RLF		TMPDATA,F				;move row up two bits 
				RLF		TMPDATA,W
				ADDWF	KEYCOL,W				;form address
				ANDLW	00FH					;mask off upper nibble
				CALL	KEYTABLE				;call look up table
				
				MOVWF	WRITECHAR	
				CALL	WRITETMP
				CALL 	LCDLINE2
				CALL	PRNTMPARY
				MOVFW	WRITEPOINTER
				SUBLW	07H
				BTFSS	STATUS,Z
				INCF	WRITEPOINTER,F
				
SKIPSTAR		MOVFW	KEYROW					;reset MUX for button de bounce test
				MOVWF	PORTD
				BSF		PORTE,MUXEN				

WAITCOL1		BTFSS	PORTD,4 				;if the button is still depressed then wait.
				GOTO	WAITCOL1
				CALL	QDELAY
				BTFSS	PORTD,4					;de-bounce key release	
				GOTO	WAITCOL1
				
;---------------|-------|-----------------------|------------------------------------------------				
COL2			INCF	KEYCOL,F				;Increment to next column
				BTFSC	PORTD,5
				GOTO	COL3
				CALL	QDELAY
				BTFSC	PORTD,5					;de-bounce key press	
				GOTO	COL3

				MOVFW	TMP7					;Abort array addition if array full
				XORLW	A' '
				BTFSS	STATUS,Z
				RETURN
				
				MOVFW	KEYROW					;Start Col2 check
				MOVWF	TMPDATA
				BCF     STATUS,C
				RLF		TMPDATA,F
				RLF		TMPDATA,W
				ADDWF	KEYCOL,W
				ANDLW	00FH
				CALL	KEYTABLE
											
				MOVWF	WRITECHAR				;Add new 
				CALL	WRITETMP
				CALL 	LCDLINE2	
				CALL	PRNTMPARY
				MOVFW	WRITEPOINTER
				SUBLW	07H
				BTFSS	STATUS,Z
				INCF	WRITEPOINTER,F
				
				MOVFW	KEYROW
				MOVWF	PORTD
				BSF		PORTE,MUXEN
	

WAITCOL2		BTFSS	PORTD,5 				;if the button is still depressed then wait.
				GOTO	WAITCOL2
				CALL	QDELAY
				BTFSS	PORTD,5					;de-bounce key release	
				GOTO	WAITCOL2
				
;---------------|-------|-----------------------|------------------------------------------------				
COL3			INCF	KEYCOL,F
				BTFSC	PORTD,6
				RETURN
				CALL	QDELAY
				BTFSC	PORTD,6					;Debouch Key Press	
				RETURN
				
				MOVFW	KEYROW					;Check if row is 3 for # test
				SUBLW	03H
				BTFSS	STATUS,Z
				GOTO	NOTHASH					;If Row3 and Col1 then # has been pressed
				BSF		SYSFLAGS_A,KEYFIN
				GOTO	SKIPHASH
				
NOTHASH			MOVFW	TMP7					;Abort array addition if array full
				XORLW	A' '
				BTFSS	STATUS,Z
				RETURN
				
				MOVFW	KEYROW					;key row is either 00,01,10,11
				MOVWF	TMPDATA
				BCF     STATUS,C
				RLF		TMPDATA,F				;rotate 00->0000,01->0100,10->1000,11->1100
				RLF		TMPDATA,W
				ADDWF	KEYCOL,W				;key column is either 00,01,10 
				ANDLW	00FH					;form address and mask 
				CALL	KEYTABLE
				
				MOVWF	WRITECHAR
				CALL	WRITETMP
				CALL 	LCDLINE2
				CALL	PRNTMPARY
				MOVFW	WRITEPOINTER
				SUBLW	07H
				BTFSS	STATUS,Z
				INCF	WRITEPOINTER,F
				
SKIPHASH		MOVFW	KEYROW
				MOVWF	PORTD
				BSF		PORTE,MUXEN
				
WAITCOL3		BTFSS	PORTD,6					;if the button is still depressed then wait.
				GOTO	WAITCOL3
				CALL	QDELAY
				BTFSS	PORTD,6					;de-bounce key release	
				GOTO	WAITCOL3	
				RETURN
				
;---------------|-------|-----------------------|------------------------------------------------
;PRINT TEMP ARRAY
;---------------|-------|-----------------------|------------------------------------------------					
PRNTMPARY		MOVLW	D'08'
				MOVWF	GPFILE0
				
				MOVFW	TMP1
				XORLW	A' '
				BTFSS	STATUS,Z
				DECF	GPFILE0,F
							
				MOVFW	TMP3
				XORLW	A' '
				BTFSS	STATUS,Z
				DECF	GPFILE0,F

				MOVFW	TMP5
				XORLW	A' '
				BTFSS	STATUS,Z
				DECF	GPFILE0,F

				MOVFW	TMP7
				XORLW	A' '
				BTFSS	STATUS,Z
				DECF	GPFILE0,F
				
RESPACE			MOVLW	A' '
				CALL	DTOG
				DECF	GPFILE0,F
				BTFSS	STATUS,Z
				GOTO	RESPACE
				
				BTFSC	SYSFLAGS_B,HIDEINKEYS
				GOTO	HIDEARY
				
SHOWARY			MOVFW	TMP0
				CALL	DTOG
				MOVFW	TMP1
				CALL	DTOG
				MOVFW	TMP2
				CALL	DTOG
				MOVFW	TMP3
				CALL	DTOG
				MOVFW	TMP4
				CALL	DTOG
				MOVFW	TMP5
				CALL	DTOG
				MOVFW	TMP6
				CALL	DTOG
				MOVFW	TMP7
				CALL	DTOG
				RETURN
				
HIDEARY			MOVFW	TMP0					;
				XORLW	A' '
				BTFSS	STATUS,Z
				CALL 	PRNTMPSTR
				
				MOVFW	TMP1					;
				XORLW	A' '
				BTFSS	STATUS,Z
				CALL 	PRNTMPSTR
				
				MOVFW	TMP2					;
				XORLW	A' '
				BTFSS	STATUS,Z
				CALL 	PRNTMPSTR
				
				MOVFW	TMP3					;
				XORLW	A' '
				BTFSS	STATUS,Z
				CALL 	PRNTMPSTR
				
				MOVFW	TMP4					;
				XORLW	A' '
				BTFSS	STATUS,Z
				CALL 	PRNTMPSTR
				
				MOVFW	TMP5					;
				XORLW	A' '
				BTFSS	STATUS,Z
				CALL 	PRNTMPSTR
				
				MOVFW	TMP6					;
				XORLW	A' '
				BTFSS	STATUS,Z
				CALL 	PRNTMPSTR
				
				MOVFW	TMP7					;
				XORLW	A' '
				BTFSS	STATUS,Z
				CALL 	PRNTMPSTR
				
				MOVLW	A' '					;Include trailing blank to clean up display
				CALL	DTOG

				RETURN
	
PRNTMPSTR		MOVLW	A'*'
				CALL	DTOG
				RETURN
				
;---------------|-------|-----------------------|------------------------------------------------
;CLR TMP ARRAY
;---------------|-------|-----------------------|------------------------------------------------					
CLRTMPARY		CLRF	WRITEPOINTER
NXTLOC			MOVLW	H'20'
				MOVWF	WRITECHAR
				CALL	WRITETMP
				INCF	WRITEPOINTER,F
				MOVFW	WRITEPOINTER
				SUBLW	H'08'
				BTFSS	STATUS,Z
				GOTO	NXTLOC
				RETURN

;---------------|-------|-----------------------|------------------------------------------------
;WRITE INTO TMP ARRAY
;---------------|-------|-----------------------|------------------------------------------------				
WRITETMP		MOVFW	WRITEPOINTER
				SUBLW	00H
				BTFSS	STATUS,Z
				GOTO	TST1
				MOVFW	WRITECHAR
				MOVWF	TMP0
				RETURN
				
TST1			MOVFW	WRITEPOINTER
				SUBLW	01H
				BTFSS	STATUS,Z
				GOTO	TST2
				MOVFW	WRITECHAR
				MOVWF	TMP1
				RETURN

TST2			MOVFW	WRITEPOINTER
				SUBLW	02H
				BTFSS	STATUS,Z
				GOTO	TST3
				MOVFW	WRITECHAR
				MOVWF	TMP2
				RETURN
				
TST3			MOVFW	WRITEPOINTER
				SUBLW	03H
				BTFSS	STATUS,Z
				GOTO	TST4
				MOVFW	WRITECHAR
				MOVWF	TMP3
				RETURN		

TST4			MOVFW	WRITEPOINTER
				SUBLW	04H
				BTFSS	STATUS,Z
				GOTO	TST5
				MOVFW	WRITECHAR
				MOVWF	TMP4
				RETURN
				
TST5			MOVFW	WRITEPOINTER
				SUBLW	05H
				BTFSS	STATUS,Z
				GOTO	TST6
				MOVFW	WRITECHAR
				MOVWF	TMP5
				RETURN
				
TST6			MOVFW	WRITEPOINTER
				SUBLW	06H
				BTFSS	STATUS,Z
				GOTO	TST7
				MOVFW	WRITECHAR
				MOVWF	TMP6
				RETURN
				
TST7			MOVFW	WRITEPOINTER
				SUBLW	07H
				BTFSS	STATUS,Z
				RETURN
				MOVFW	WRITECHAR
				MOVWF	TMP7
				RETURN
				
;---------------|-------|-----------------------|------------------------------------------------
;Beep High Sub
;---------------|-------|-----------------------|------------------------------------------------
HBEEP			BSF 	PORTB,STATECHG	
				MOVLW	01FH			     		
				CALL  	VDELAY
				BCF 	PORTB,STATECHG
				RETURN
				
;				MOVLW	04h						;PRESELECT PIEZO SOUNDER PIN ON MUX
;				MOVWF	PORTD
;				MOVLW	0FFH              		;LOAD FF INTO THE W REG
;				MOVWF	CFILE1            		;MOVE W TO REG 0C
;HRELOAD		BSF		PORTE,MUXEN
;				MOVLW	03FH              		;LOAD FF INTO THE W REG
;				MOVWF	CFILE0            		;MOVE W TO REG 0D
;HLOOPOD		DECFSZ	CFILE0,F          		;DEC REG 0D JUMP IF ZERO
;				GOTO	HLOOPOD           		;GOTO LOOP0D
;				MOVF	CFILE1,W       			;
;				BCF		PORTE,MUXEN
;HLOOPOC		DECFSZ	CFILE1,F       		   	;DEC REG 0C JUMP IF ZERO
;				GOTO	HRELOAD					;RELOAD REG 0D
;				BCF		PORTE,MUXEN
;				RETURN

;---------------|-------|-----------------------|------------------------------------------------
;FAST HEX TO DEC CONVERSION  v3.00
;---------------|-------|-----------------------|------------------------------------------------
HDEC			CLRF	CPOINTER
				CLRF	EHUNS
				CLRF	ETENS
				CLRF	EUNIT
				CLRF	CINDEX
				BSF		STATUS,CARRY
				RLF		CINDEX,F
				GOTO	SCANSTART
				
SCANCNTR		BCF		STATUS,CARRY
				RLF		CINDEX,F

SCANSTART		MOVFW	CINDEX
				ANDWF	HEXBT,W
				BTFSC	STATUS,Z
				GOTO	CHKNEXT
				CALL	LOADBWEIGHT
				CALL	HDECADD	

CHKNEXT			INCF	CPOINTER,F
				BTFSS	CINDEX,7
				GOTO	SCANCNTR
				RETURN
				
LOADBWEIGHT		MOVFW	CPOINTER
				CALL	CONTABLE
				MOVWF	AUNIT
				MOVFW	CPOINTER
				ADDLW	08H
				CALL	CONTABLE
				MOVWF	ATENS
				CLRF	AHUNS
				BTFSC	CINDEX,7
				INCF	AHUNS,F
				RETURN
			
HDECADD			MOVFW	AUNIT
				ADDWF	EUNIT,W
				MOVWF	EUNIT
				SUBLW	0AH						;14 - 10 = 4 OR 4 - 10 = -6	
				BTFSS	STATUS,CARRY 
				CALL	UNITCARRY1
				BTFSC	STATUS,Z 
				CALL	UNITCARRY1
				
				MOVFW	ATENS
				ADDWF	ETENS,W
				MOVWF	ETENS
				SUBLW	0AH						;14 - 10 = 4 OR 4 - 10 = -6	
				BTFSS	STATUS,CARRY 
				CALL	TENSCARRY1
				BTFSC	STATUS,Z 
				CALL	TENSCARRY1

				MOVFW	AHUNS
				ADDWF	EHUNS,W
				MOVWF	EHUNS
				SUBLW	0AH						;14 - 10 = 4 OR 4 - 10 = -6	
				BTFSS	STATUS,CARRY 
				CALL	HUNSCARRY1
				BTFSC	STATUS,Z 
				CALL	HUNSCARRY1
				RETURN
				
UNITCARRY1		MOVLW	0AH
				SUBWF	EUNIT,F
				INCF	ETENS,F
				RETURN
				
TENSCARRY1		MOVLW	0AH
				SUBWF	ETENS,F
				INCF	EHUNS,F
				RETURN
				
HUNSCARRY1		MOVLW	0AH
				SUBWF	EHUNS,F
				RETURN
				
;---------------|-------|-----------------------|------------------------------------------------
;HEX ASCII CONVERSION
;---------------|-------|-----------------------|------------------------------------------------
HASC			ANDLW	B'00001111'
				MOVWF	WKFILE
				BTFSS  	WKFILE,3				;CONVERT TO ASCII
				GOTO 	ADDD30
				BTFSC  	WKFILE,2
				GOTO 	ADDD37
				BTFSS  	WKFILE,1
				GOTO 	ADDD30

ADDD37			ADDLW	37H						;ADD IF ABOVE 9
				RETURN

ADDD30			ADDLW	30H
				RETURN

;---------------|-------|-----------------------|------------------------------------------------
;Variable Delay Sub
;---------------|-------|-----------------------|------------------------------------------------
MDELAY			MOVLW	0FFH
VDELAY  		MOVWF	CFILE1         		   	;MOVE W TO REG 0C
RELOAD    		MOVLW	0FFH      	        	;LOAD FF INTO THE W REG
				MOVWF	CFILE0    	        	;MOVE W TO REG 0D
LOOPOD    		DECFSZ	CFILE0,F  	        	;DEC REG 0D JUMP IF ZERO
				GOTO	LOOPOD    	        	;GOTO LOOP0D
				MOVF	CFILE1,W
LOOPOC    		DECFSZ	CFILE1,F	          	;DEC REG 0C JUMP IF ZERO
				GOTO	RELOAD      	      	;RELOAD REG 0D
				RETURN

;---------------|-------|-----------------------|------------------------------------------------
;Small fast delay
;---------------|-------|-----------------------|------------------------------------------------
QDELAY  		MOVLW   0FFH            		;LOAD FF INTO THE W REG
				MOVWF   CFILE0          		;MOVE W TO REG 0D
LOOPODY 		DECFSZ  CFILE0,F        		;DEC REG 0D JUMP IF ZERO
				GOTO    LOOPODY         		;GOTO LOOP0D
				RETURN 

;---------------|-------|-----------------------|------------------------------------------------
;EEPROM READ
;---------------|-------|-----------------------|------------------------------------------------
EREAD			CALL	PAGE2
				MOVF 	FADDR,W 				;
				MOVWF 	EEADR^100H		 		;Data Memory Address to read
				CALL	PAGE3
				BCF 	EECON1^180H,EEPGD 		;Point to DATA memory
				BSF 	EECON1^180H,ERD 		;EEPROM Read
				CALL 	PAGE2
				MOVF 	EEDATA^100H,W 			;W = EEDATA		
				CALL	PAGE0
				MOVWF	FDATA
				RETURN

;---------------|-------|-----------------------|------------------------------------------------
;EEPROM WRITE
;---------------|-------|-----------------------|------------------------------------------------
EWRITE		MOVF 	FADDR,W 					;		              
        	CALL	PAGE2   
			MOVWF   EEADR^100H          	    ;set address 
     		CALL	PAGE0
			MOVF 	FDATA,W         			;      
        	CALL	PAGE2
			               
        	MOVWF   EEDATA^100H           		;set data                        
			CALL	PAGE3	
        	                     
        	BCF     EECON1^180H,EEPGD    		;select data memory             
        	BSF     EECON1^180H,WREN    	 	;enable writes                   
        	BCF     INTCON1^180H,GIE    	  	;disable interrupts             
	        MOVLW   h'55'           			;                                 
        	MOVWF   EECON2^180H      	     	;                                 
        	MOVLW   h'AA'         			  	;                                 
        	MOVWF   EECON2^180H        		   	;                                 
        	BSF     EECON1^180H,WR     		  	;initiate the write              

WRITEOK		BTFSC   EECON1^180H,WR      	 	;last write complete?            
        	GOTO    WRITEOK          			;no, branch                      
        	BSF     INTCON1^180H ,GIE     	 	;enable interrupts               
        	BCF     EECON1^180H ,WREN    	 	;disable writes                  
        	CALL	PAGE0              
        	RETURN			
			
;---------------|-------|-----------------------|------------------------------------------------
;PAGE CHANGE CALLS
;---------------|-------|-----------------------|------------------------------------------------
PAGE0			BCF   	STATUS,5
				BCF   	STATUS,6
				RETURN
PAGE1			BSF   	STATUS,5
				BCF   	STATUS,6
				RETURN	
PAGE2			BCF   	STATUS,5
				BSF   	STATUS,6
				RETURN		
PAGE3			BSF   	STATUS,5
				BSF   	STATUS,6
				RETURN	
				
;---------------|-------|-----------------------|------------------------------------------------
;Read Pre-sets from Flash
;---------------|-------|-----------------------|------------------------------------------------
RDFLSH			MOVLW	00H						;Load pre-sets
				MOVWF	FADDR
				CALL	EREAD
				MOVF	FDATA,W
				MOVWF	EXTTIME
				
				MOVLW	01H						;Load pre-sets
				MOVWF	FADDR
				CALL	EREAD
				MOVF	FDATA,W
				MOVWF	ENTTIME	
				
				MOVLW	02H						;Load pre-sets
				MOVWF	FADDR
				CALL	EREAD
				MOVF	FDATA,W
				MOVWF	ALMTIME	
					
EPWRDSTRTA  	MOVLW   08H   					;loop reads flash 0A - 03 to register 40H - 39H       		
				MOVWF   CFILE0          		;load 8 into file	
				
EPWRDA	 		DECF	CFILE0,F				;decrement to give 7
				MOVFW   CFILE0
				ADDLW	03H						;add 7+3 = 10b = Ah starting flash address
				MOVWF	FADDR					;move to flash pointer	
				CALL	EREAD
				MOVFW   CFILE0					;Load 7+39 = 40h into pointer register	
				ADDLW	039H
				MOVWF	FSR	
				BCF		STATUS,IRP				;force bank selection
				MOVF	FDATA,W					;transfer flash data into working				
				MOVWF	INDF					;move flash data into target register 
				
				MOVFW	CFILE0 					;loop checks to see if finished
				BTFSS  	STATUS,Z
				GOTO    EPWRDA   
				
EPWRDSTRTB  	MOVLW   08H   					;loop reads flash 12 - 0B to register 48H - 41H      		
				MOVWF   CFILE0          		;load 8 into file	
				
EPWRDB	 		DECF	CFILE0,F				;decrement to give 7
				MOVFW   CFILE0
				ADDLW	0BH						;add 7+11 = 18d = 12h starting flash address
				MOVWF	FADDR					;move to flash pointer	
				CALL	EREAD
				MOVFW   CFILE0					;Load 7+41 = 48h into pointer register	
				ADDLW	041H
				MOVWF	FSR	
				BCF		STATUS,IRP				;force bank selection
				MOVF	FDATA,W					;transfer flash data into working				
				MOVWF	INDF					;move flash data into target register 
				
				MOVFW	CFILE0 					;loop checks to see if finished
				BTFSS  	STATUS,Z
				GOTO    EPWRDB  				
				RETURN

;---------------|-------|-----------------------|------------------------------------------------
;Write Pre-sets to Flash
;---------------|-------|-----------------------|------------------------------------------------
WRFLSH			MOVLW	00H						;Load pre-sets
				MOVWF	FADDR
				MOVFW	EXTTIME
				MOVWF	FDATA
				CALL	EWRITE
				
				MOVLW	01H						;Load pre-sets
				MOVWF	FADDR
				MOVFW	ENTTIME
				MOVWF	FDATA
				CALL	EWRITE
				
				MOVLW	02H						;Load pre-sets
				MOVWF	FADDR
				MOVFW	ALMTIME
				MOVWF	FDATA
				CALL	EWRITE
								
EPWRSTRTA  		MOVLW   08H   					;loop write flash 0A - 03 to register 40H - 39H       		
				MOVWF   CFILE0     		     	;load 8 into file	
				
EPWWRA	 		DECF	CFILE0,F				;decrement to give 7
				MOVFW   CFILE0
				ADDLW	03H						;add 7+3 = 10d = Ah starting flash address
				MOVWF	FADDR					;move to flash pointer	
				
				MOVFW   CFILE0					;Load 7+31 = 38 into pointer register	
				ADDLW	039H
				MOVWF	FSR	
				BCF		STATUS,IRP				;force bank selection
				MOVFW	INDF					; 
				MOVWF	FDATA					;				
				CALL	EWRITE
				
				MOVFW	CFILE0 					;loop checks if zero end flash save
				BTFSS  	STATUS,Z
				GOTO    EPWWRA  

EPWRSTRTB  		MOVLW   08H   					;loop write flash 12 - 0B to register 48H - 41H       		
				MOVWF   CFILE0     		     	;load 8 into file	
				
EPWWRB	 		DECF	CFILE0,F				;decrement to give 7
				MOVFW   CFILE0
				ADDLW	0BH						;add 7+11 = 18d = 12h starting flash address
				MOVWF	FADDR					;move to flash pointer	
				
				MOVFW   CFILE0					;Load 7+31 = 38 into pointer register	
				ADDLW	041H
				MOVWF	FSR	
				BCF		STATUS,IRP				;force bank selection
				MOVFW	INDF					; 
				MOVWF	FDATA					;				
				CALL	EWRITE
				
				MOVFW	CFILE0 					;loop checks if zero end flash save
				BTFSS  	STATUS,Z
				GOTO    EPWWRB  				

				
				RETURN
					
;---------------|-------|-----------------------|------------------------------------------------
;Check flash contents from 00 to 12 for FF's, return 1 if in error and 0 if OK
;---------------|-------|-----------------------|------------------------------------------------
CHKFLSH			BSF		SYSFLAGS_A,FLASHBAD
				MOVLW   013H   					;loop reads flash 12 - 00 to check for FF's       		
				MOVWF   CFILE0          	
				
CEPWLO	 		DECF	CFILE0,F				;decrement to give A
				MOVFW   CFILE0
				MOVWF	FADDR					;move to flash pointer	
				CALL	EREAD
				
				MOVF	FDATA,W					;transfer flash data into working
				XORLW	0FFH
				BTFSC	STATUS,Z
				RETURN
				
				MOVFW	CFILE0 					;loop checks
				BTFSS  	STATUS,Z
				GOTO    CEPWLO   
				BCF		SYSFLAGS_A,FLASHBAD		;Clear system flag if read is OK
				RETURN							;valid example 05,06,07,31,39,36,39,20,20,20,20
												;entry, exit, alarm, passworda, passwordb
												;XX XX XX XXXXXXXX XXXXXXXX

				
	END